package com.ws.dao;


import com.ws.entity.*;

import java.util.InputMismatchException;
import java.util.Scanner;

public class BikeManager {
    private SharedBikeCompany[] companies;
    private final Scanner scanner = new Scanner(System.in);

    public void addData() {
        // 初始化单车公司和单车信息
        OfOcompany ofo = new OfOcompany();
        ofo.setBikeCompanyId(1);
        ofo.setBikeCompanyName("ofo");
        SharedBike[] ofoBikes = {
                SharedBike.createBike(1, "ofo", 1),
                SharedBike.createBike(2, "ofo", 0).setBorrowTime("2024-01-01 09:00")
        };
        ofo.setSharedBikes(ofoBikes);
        ofo.setSum(2);
        ofo.setCount(10);

        Hellocompany haro = new Hellocompany();
        haro.setBikeCompanyId(2);
        haro.setBikeCompanyName("哈罗");
        SharedBike[] haroBikes = {
                SharedBike.createBike(3, "哈罗", 1),
                SharedBike.createBike(4, "哈罗", 0).setBorrowTime("2024-01-02 13:00")
        };
        haro.setSharedBikes(haroBikes);
        haro.setSum(2);
        haro.setCount(15);

        Qingjucompany qingju = new Qingjucompany();
        qingju.setBikeCompanyId(3);
        qingju.setBikeCompanyName("摩拜");
        SharedBike[] qingjuBikes = {
                SharedBike.createBike(5, "摩拜", 1),
                SharedBike.createBike(6, "摩拜", 0).setBorrowTime("2024-01-03 15:00")
        };
        qingju.setSharedBikes(qingjuBikes);
        qingju.setSum(2);
        qingju.setCount(20);

        companies = new SharedBikeCompany[]{ofo, haro, qingju};
    }

    // 查看单车信息

    public void viewBike() {
        for (int i = 0; i < companies.length; i++) {
            SharedBikeCompany company = companies[i];
            if (company != null) {
                System.out.println("-----------------------------------------------");
                System.out.println("|公司序号 | 公司名称 | 公司单车数量 | 公司单车借出次数|");
                System.out.println("-----------------------------------------------");
                System.out.println("|" + (i + 1) + " \t\t| " + company.getBikeCompanyName() + " \t | " + company.getSum() + " \t\t| " + company.getCount() + "|");
                System.out.println("-----------------------------------------------");
                System.out.println("单车序号 | 单车品牌 | 单车状态 | 借出时间");
                System.out.println("-----------------------------------------------");
                for (int j = 0; j < company.getSharedBikes().length; j++) {
                    SharedBike bike = company.getSharedBikes()[j];
                    if (bike != null) {
                        String status = bike.getStatus() == 1 ? "可借" : "已借出";
                        System.out.println("|" + (j + 1) + " \t| " + bike.getBname() + " \t| " + status + " \t| " + bike.getBorrowTime() + "|");
                        System.out.println("-----------------------------------------------");
                    }
                }
            }
        }
    }
    //投放单车
    public void dropBike() {
        while (true) {
            System.out.println("请选择你要投放的单车公司:");
            System.out.println("1——" + companies[0].getBikeCompanyName());
            System.out.println("2——" + companies[1].getBikeCompanyName());
            System.out.println("3——" + companies[2].getBikeCompanyName());
            System.out.println("0——返回主菜单");
            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 1:
                        ((BikeDropService) companies[0]).dropBike(companies[0], scanner);
                        break;
                    case 2:
                        ((BikeDropService) companies[1]).dropBike(companies[1], scanner);
                        break;
                    case 3:
                        ((BikeDropService) companies[2]).dropBike(companies[2], scanner);
                        break;
                    case 0:
                        return; // 返回主菜单
                    default:
                        System.out.println("你输入的数据错误");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
        }
    }
    //删除单车
    public void deleteBike() {
        while (true) {
            System.out.println("请选择你要删除单车的公司:");
            System.out.println("1——" + companies[0].getBikeCompanyName());
            System.out.println("2——" + companies[1].getBikeCompanyName());
            System.out.println("3——" + companies[2].getBikeCompanyName());
            System.out.println("0——返回主菜单");
            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 1:
                        ((BikeDeleteService) companies[0]).deleteBike(companies[0], scanner);
                        break;
                    case 2:
                        ((BikeDeleteService) companies[1]).deleteBike(companies[1], scanner);
                        break;
                    case 3:
                        ((BikeDeleteService) companies[2]).deleteBike(companies[2], scanner);
                        break;
                    case 0:
                        return; // 返回主菜单
                    default:
                        System.out.println("你输入的数据错误");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
        }
    }
    //借出单车
    public void loanBike() {
    }
    //归还单车
    public void returnBike() {
    }
    //单车排行榜
    public void bikeranking() {
    }
}